package com.hxkcloud.patient.dborm;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.hxkcloud.patient.beans.ArticleKindBean;
import com.hxkcloud.patient.beans.ArticleKindChildBean;
import com.hxkcloud.patient.beans.BasicInfoTimeBean;
import com.hxkcloud.patient.beans.DepartmentBean;
import com.hxkcloud.patient.beans.DiseaseBean;
import com.hxkcloud.patient.beans.DoctorGroupBean;
import com.hxkcloud.patient.beans.HospitalStatusBean;
import com.hxkcloud.patient.beans.LoginInfo;
import com.hxkcloud.patient.beans.AddressBean;
import com.hxkcloud.patient.beans.LogininfoVerifyBean;
import com.hxkcloud.patient.beans.MonitorKindBean;
import com.hxkcloud.patient.beans.MonitorKindTestBean;
import com.hxkcloud.patient.beans.MonitorKindValueBean;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by william_sun on 2016-07-21.
 * Description:http://blog.csdn.net/lmj623565791/article/details/39121377
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    private static final String DB_NAME = "haoxinkcz.db";
    private static final int DB_VER = 3;
    private static DatabaseHelper instance;
    private Map<String, Dao> daos = new HashMap<String, Dao>();

    private DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VER);
    }

//    public DatabaseHelper(Context context, String databaseName, SQLiteDatabase.CursorFactory factory, int databaseVersion) {
//        super(context, DB_NAME, null, DB_VER);
//    }
//
//    public DatabaseHelper(Context context, String databaseName, SQLiteDatabase.CursorFactory factory, int databaseVersion, int configFileId) {
//        super(context, DB_NAME, factory, DB_VER, configFileId);
//    }
//
//    public DatabaseHelper(Context context, String databaseName, SQLiteDatabase.CursorFactory factory, int databaseVersion, File configFile) {
//        super(context, DB_NAME, factory, DB_VER, configFile);
//    }
//
//    public DatabaseHelper(Context context, String databaseName, SQLiteDatabase.CursorFactory factory, int databaseVersion, InputStream stream) {
//        super(context, DB_NAME, factory, DB_VER, stream);
//    }

    /**
     * 单例获取该Helper
     *
     * @param context
     * @return
     */
    public static synchronized DatabaseHelper getHelper(Context context) {
        if (instance == null) {
            synchronized (DatabaseHelper.class) {
                if (instance == null)
                    instance = new DatabaseHelper(context);
            }
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, LoginInfo.class);
            TableUtils.createTable(connectionSource, AddressBean.class);
            TableUtils.createTable(connectionSource, DepartmentBean.class);
            TableUtils.createTable(connectionSource, DiseaseBean.class);
            TableUtils.createTable(connectionSource, HospitalStatusBean.class);
            TableUtils.createTable(connectionSource, MonitorKindBean.class);
            TableUtils.createTable(connectionSource, MonitorKindValueBean.class);
            TableUtils.createTable(connectionSource, MonitorKindTestBean.class);
            TableUtils.createTable(connectionSource, ArticleKindBean.class);
            TableUtils.createTable(connectionSource, ArticleKindChildBean.class);
            TableUtils.createTable(connectionSource, BasicInfoTimeBean.class);
            TableUtils.createTable(connectionSource, LogininfoVerifyBean.class);
            TableUtils.createTable(connectionSource, DoctorGroupBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        try {
            TableUtils.dropTable(connectionSource, LoginInfo.class, true);
            TableUtils.dropTable(connectionSource, AddressBean.class, true);
            TableUtils.dropTable(connectionSource, DepartmentBean.class, true);
            TableUtils.dropTable(connectionSource, DiseaseBean.class, true);
            TableUtils.dropTable(connectionSource, HospitalStatusBean.class, true);
            TableUtils.dropTable(connectionSource, MonitorKindBean.class, true);
            TableUtils.dropTable(connectionSource, MonitorKindValueBean.class, true);
            TableUtils.dropTable(connectionSource, MonitorKindTestBean.class, true);
            TableUtils.dropTable(connectionSource, ArticleKindBean.class, true);
            TableUtils.dropTable(connectionSource, ArticleKindChildBean.class, true);
            TableUtils.dropTable(connectionSource, BasicInfoTimeBean.class, true);
            TableUtils.dropTable(connectionSource, LogininfoVerifyBean.class, true);
            TableUtils.dropTable(connectionSource, DoctorGroupBean.class, true);
            onCreate(sqLiteDatabase, connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public synchronized Dao getDao(Class clazz) throws SQLException {
        Dao dao = null;
        String className = clazz.getSimpleName();

        if (daos.containsKey(className)) {
            dao = daos.get(className);
        }
        if (dao == null) {
            dao = super.getDao(clazz);
            daos.put(className, dao);
        }
        return dao;
    }

    /**
     * 释放资源
     */
    @Override
    public void close() {
        super.close();
        for (String key : daos.keySet()) {
            Dao dao = daos.get(key);
            dao = null;
        }
    }
}
